package com.bdqn.controller;

import com.bdqn.pojo.SysRoleUser;
import com.bdqn.pojo.User;
import com.bdqn.service.UserService;
import com.bdqn.util.FileUploadUtil;
import com.bdqn.util.TokenUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin  //允许跨域访问
@RequestMapping("/user")
@Tag(name = "用户控制器",description = "用户CRUD")
public class UserController {
    @Autowired
    FileUploadUtil fileUploadUtil;
    @Autowired
    UserService userService;
    @Autowired
    TokenUtil tokenUtil;
    /**
     * 登录验证，根据用户名和密码查询用户
     */
    @Operation(summary = "checkLogin",description = "验证登录")
    @Parameter(name = "map",description = "用户名和密码的map集合",required = true)
    @PostMapping("checkLogin")
    public String checkLogin(@RequestBody Map<String,String> map){
        String token=null;
        //获取用户名和密码
        String uname=map.get("username");
        String pwd=map.get("password");
        //调用service,根据用户名和密码获取用户
        User user=userService.getUser(uname,pwd);
        if (user!=null){
            //登录成功，生成加密token
            token=tokenUtil.createToken(user);
        }
        return token;
    }
    /**
     * 根据token获取用户对象
     */
    @Operation(summary = "getUser",description = "查找用户")
    @Parameter(name = "uid",description = "用户id",required = true)
    @GetMapping("/getUser")
    public User getUser(@RequestHeader("token") String token){
        int uid=tokenUtil.getUid(token);
        User user=userService.getUser(uid);
        return user;
    }

    /**
     * 获取当前页的用户相关的数据
     */
    @GetMapping("/getUsers")
    public PageInfo<User> getUsers(int pageNum,int pageSize,String uname){
        PageInfo<User> pageInfo=userService.selectPage(pageNum,pageSize,uname);
        return pageInfo;
    }

    /**
     * 用户头像的上传到阿里云oss服务器
     */
    @PostMapping("/uploadImg")
    public String uploadImg(MultipartFile file){
        String url="";
        try {
            url=fileUploadUtil.upload(file);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return url;
    }

    /**
     * 添加用户
     */
    @PostMapping("addUser")
    public int addUser(@RequestBody User user){
        int row=userService.addUser(user);
        return row;
    }

    /**
     * 添加用户的角色
     */
    @GetMapping("addUserRole")
    public int addUserRole(int uid,
                           @RequestParam("rids")List<Integer> rids){
        int row=userService.addUserRole(uid,rids);
        return row;
    }
//    查询所有医生
    @GetMapping("getUserAll")
    public List<User> getUserAll(){
        List<User> list=userService.getUser();
        return list;

    }
}
